import { CrudOptions, AddReq, DelReq, EditReq, dict, CrudExpose, UserPageQuery, CreateCrudOptionsRet, ValueResolveContext, ValueBuilderContext  } from '@fast-crud/fast-crud';
import _ from 'lodash-es';
import * as api from './api';
import { request } from '/@/utils/service';
import { auth } from '/@/utils/authFunction';


//此处为crudOptions配置
export default function ({ crudExpose }: { crudExpose: CrudExpose }): CreateCrudOptionsRet {
	const pageRequest = async (query: any) => {
		return await api.GetList(query);
	};
	const editRequest = async ({ form, row }: EditReq) => {
		if (row.id) {
			form.id = row.id;
		}
		return await api.UpdateObj(form);
	};
	const delRequest = async ({ row }: DelReq) => {
		return await api.DelObj(row.id);
	};
	const addRequest = async ({ form }: AddReq) => {
		return await api.AddObj(form);
	};

	const exportRequest = async (query: UserPageQuery) => {
		return await api.exportData(query);
	};

	return {
		crudOptions: {
			request: {
				pageRequest,
				addRequest,
				editRequest,
				delRequest,
			},
			actionbar: {
				buttons: {
					export: {
						// 注释编号:django-vue3-admin-crud210716:注意这个auth里面的值，最好是使用index.vue文件里面的name值并加上请求动作的单词
						show: auth('SaleDataViewSet:Export'),
						text: '导出', //按钮文字
						title: '导出', //鼠标停留显示的信息
						click() {
							return exportRequest(crudExpose.getSearchFormData());
							// return exportRequest(crudExpose!.getSearchFormData())    // 注意这个crudExpose!.getSearchFormData()，一些低版本的环境是需要添加!的
						},
					},
					add: {
						show: auth('SaleDataViewSet:Create'),
						text: '新增', //按钮文字
						title: '新增', //鼠标停留显示的信息
					},
				},
			},
			rowHandle: {
				//固定右侧
				fixed: 'right',
				width: 300,
				buttons: {
					view: {
						type: '',
						order: 1,
						show: auth('SaleDataViewSet:Retrieve'),
					},
					edit: {
						type: 'success',
						order: 2,
						show: auth('SaleDataViewSet:Update'),
					},
					copy: {
						type: 'primary',
						order: 3,
						show: auth('SaleDataViewSet:Copy'),
					},
					remove: {
						type: 'danger',
						order: 4,
						show: auth('SaleDataViewSet:Delete'),
					},
				},
			},
			columns: {
				
				department: {
					title: '组织名称',
					search: {
						show: true,
					},
					type: 'dict-tree',
					dict: dict({
						isTree: true,
						url: '/api/system/dept/all_dept/',
						value: 'id',
						label: 'name',
					}),
					column: {
						width: 200,
					},
					form: {
						rules: [
							// 表单校验规则
							{
								required: true,
								message: '必填项',
							},
						],
						component: {
							filterable: true,
							placeholder: '请选择',
							props: {
								checkStrictly: true,
								props: {
									value: 'id',
									label: 'name',
								},
							},
						},
					},
				},
				sale_time: {
					title: '销售日期',
					type: 'date',
					search: { show: true },
					form: {
						rules: [{ required: true, message: '销售日期必填' }],
						component: {
							//显示格式化
							format: 'YYYY-MM-DD',
							//输入值格式
							valueFormat: 'YYYY-MM-DD',
							placeholder: '请输入销售日期',
						},
					},
					column: {
						align: 'center',
						component: { name: 'fs-date-format', format: 'YYYY-MM-DD' },
						width: 200,
					},
				},
				sale_data: {
					title: '销售数据',
					type: 'json',
					search: { show: false },
					column: {
						show: true,
					},
					form: {
						helper: {
							render() {
								return <div style={'color:red'}>非专业人员请勿编辑【销售数据】</div>;
							},
						},
						col: { span: 24 }, //配置在表单中此字段独占整行,
						valueBuilder({ form }: ValueBuilderContext) {
							if (form.sale_data == null) {
								return;
							}
							form.sale_data = JSON.parse(form.sale_data);
						},
						valueResolve({ form }: ValueResolveContext) {
							if (form.sale_data == null) {
								return;
							}
							form.sale_data = JSON.stringify(form.sale_data);
						},
					},
				},
				modifier_name: {
					title: '最后修改人',
					type: 'text',
					search: { show: false },
					column: {
						width: 200,
						sortable: 'custom',
					},
					form: {
						show: false,
					},
				},
				creator_name: {
					title: '创建人',
					type: 'text',
					search: { show: false },
					column: {
						width: 200,
						sortable: 'custom',
					},
					form: {
						show: false,
					},
				},
				
			},
		},
	};
}
